package com.ruoyi.web.controller;

import java.util.Date;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.TaskStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.Task;
import com.ruoyi.system.service.ITaskService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 任务Controller
 * 
 * @author ruoyi
 * @date 2024-12-26
 */
@Controller
@RequestMapping("/task/manage")
public class TaskController extends BaseController
{
    private String prefix = "task/manage";

    @Autowired
    private ITaskService taskService;

    @RequiresPermissions("task:manage:view")
    @GetMapping()
    public String manage()
    {
        return prefix + "/manage";
    }

    /**
     * 查询任务列表
     */
    @RequiresPermissions("task:manage:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Task task)
    {
        startPage();
        List<Task> list = taskService.selectTaskList(task);
        return getDataTable(list);
    }

    /**
     * 导出任务列表
     */
    @RequiresPermissions("task:manage:export")
    @Log(title = "任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Task task)
    {
        List<Task> list = taskService.selectTaskList(task);
        ExcelUtil<Task> util = new ExcelUtil<Task>(Task.class);
        return util.exportExcel(list, "任务数据");
    }

    /**
     * 新增任务
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存任务
     */
	/*
	 * @RequiresPermissions("task:manage:add")
	 * 
	 * @Log(title = "任务", businessType = BusinessType.INSERT)
	 * 
	 * @PostMapping("/add")
	 * 
	 * @ResponseBody public AjaxResult addSave(Task task) { return
	 * toAjax(taskService.insertTask(task)); }
	 */
    
    /**
     * 新增保存任务
     */
    @RequiresPermissions("task:manage:add")
    @Log(title = "任务", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Long deviceId)
    {
    	Task task = new Task();
    	task.setDeviceId(deviceId);
    	task.setState(TaskStatus.WAITING_CREATING.getCode());
    	task.setUserId(ShiroUtils.getUserId());
    	task.setGmtCreate(new Date());
    	return toAjax(taskService.insertTask(task));
    }

    /**
     * 修改任务
     */
    @RequiresPermissions("task:manage:edit")
    @GetMapping("/edit/{taskId}")
    public String edit(@PathVariable("taskId") Long taskId, ModelMap mmap)
    {
        Task task = taskService.selectTaskByTaskId(taskId);
        mmap.put("task", task);
        return prefix + "/edit";
    }

    /**
     * 修改保存任务
     */
    @RequiresPermissions("task:manage:edit")
    @Log(title = "任务", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Task task)
    {
        return toAjax(taskService.updateTask(task));
    }

    /**
     * 删除任务
     */
    @RequiresPermissions("task:manage:remove")
    @Log(title = "任务", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
//        return toAjax(taskService.deleteTaskByTaskIds(ids));
    	return toAjax(taskService.deleteTaskByTaskIdsNew(ids));
    }
}
